<?php

namespace app\merchant\job;

use app\common\controller\ActionLogs;
use app\common\controller\Platurl;
use app\common\controller\TemplateMsg;
use app\common\controller\TemplateMsgt;
use app\common\model\Agent;
use app\common\model\CardBirthdayCareLog;
use app\common\model\CardFans;
use app\common\model\CardGeneral;
use app\common\model\CardGivecardLog;
use app\common\model\CardMember;
use app\common\model\CardTimingPutUser;
use app\common\model\Merchant;
use app\common\model\Momerchant;
use app\common\model\OpenTemplate;
use app\common\model\System;
use app\common\model\WxOpen;
use think\Log;
use think\Queue;
use think\queue\Job;
use think\Session;
use Wechat\Wxobjopen;
use Wnkj\WnCardObj;

class Birthday
{

    public function fire(Job $job, $data)
    {

        $isJobDone = $this->task_put_usercare($data);
        $job->delete();

        if ($isJobDone) {
            //如果任务执行成功， 记得删除任务
            $job->delete();
            return '完成';
        } else {
            if ($job->attempts() > 3) {
                //通过这个方法可以检查这个任务已经重试了几次了
                print("<warn>任务已完成3次!" . "</warn>\n");
                $job->delete();
                // 也可以重新发布这个任务
                //print("<info>Hello Jobmach will be availabe again after 2s."."</info>\n");
                //$job->release(2); //$delay为延迟时间，表示该任务延迟2秒后再执行
            }
        }
    }



    /**
     * 根据消息中的数据进行实际的业务处理
     * @param array|mixed $data 发布任务时自定义的数据
     * @return boolean                 任务执行的结果
     */


    //群发模版消息
    public function task_put_usercare($data)
    {
        try{
            //积分
            if ((int)$data['bonus'] > 0){
                $option['card_id'] = $data['card_id'];
                $option['code'] = $data['code'];
                $option['orderid'] = '';
                $option['num'] = $data['bonus'];
                $option['bd_cate'] = 'bonus';
                $option['type'] = 1;//变动 1增加 2减少
                $option['cate'] = 1;  //变动类型
                $option['pay_cate'] = 4; //支付方式
                $option['source'] = 'web';  //来源
                $option['remark'] = $data['remark']; //备注
                $option['action_user'] = 'web';//操作人
                $Fztest = new WnCardObj($option);
                $res = $Fztest->init();
                $CardTimingPutUser = new CardTimingPutUser();
                if ($res){
                    $datas['statu'] = "Y";
                    $CardTimingPutUser->save($datas,['id'=>$data['id']]);
                }else{
                    $datas['statu'] = "E";
                    $CardTimingPutUser->save($datas,['id'=>$data['id']]);
                }
            }
            //发券
            if ($data['zs_card_id_all'] !== ''){
                $TemplateMesgt = new TemplateMsg();
                $rec_id = $this->rec_giveuserlog($data);
                $data['first'] = $data['remark'].'点击“详情”领取优惠券';
                if (strpos($data['zs_card_info'],"赠卡券：") !== false){
                    $data['zs_card_info'] = str_replace("赠卡券：",'',$data['zs_card_info']);
                }
                $data['title'] = $data['zs_card_info'];
                $data['type'] = 'birthdaycare';
                $data['url'] = '/mobile.php?s=/cardget/getcardsbirthdaycare/mid/' . $data['mid'] . '/rec_id/' . $rec_id;
                $res = $TemplateMesgt->send_tpl('put_gift',$data);
                $CardTimingPutUser = new CardTimingPutUser();
                if ($res['errcode'] == 0){
                    $datas['statu'] = "Y";
                    $CardTimingPutUser->save($datas,['id'=>$data['id']]);
                }else{
                    $datas['statu'] = "E";
                    $CardTimingPutUser->save($datas,['id'=>$data['id']]);
                }
            }
        }catch (\Exception $e){
            Log::write("task_put_usercare:".$e->getMessage() . $e->getFile() . ":" . $e->getLine(),'SysRunErrorLog');
        }
    }

    private function rec_giveuserlog($arr){
        $CardBirthdayCareLog = new CardBirthdayCareLog();
        $datas['top_mid'] = $arr['top_mid'];
        $datas['mid'] = $arr['mid'];
        $datas['c_id'] = $arr['c_id'];
        $datas['us_c_id'] = $arr['us_c_id'];
        $datas['card_id'] = $arr['card_id'];
        $datas['openid'] = $arr['openid'];
        $datas['code'] = $arr['code'];
        $datas['remark'] = $arr['remark'];
        $datas['bonus'] = $arr['bonus'];
        $datas['user_info'] = $arr['user_info'];
        $datas['put_type'] = 'birthdaycare';
        $datas['statu'] = 'N';
        $datas['zs_card_id_all'] = $arr['zs_card_id_all'];
        $datas['zs_card_info'] = $arr['zs_card_info'];
        $datas['create_time'] = time();
        $CardBirthdayCareLog->allowField(true)->save($datas);
        $give_rec_id = $CardBirthdayCareLog->id;
        $zs_card_id = $arr['zs_card_id_all'];
        if ($zs_card_id){
            $CardGivecardLog = new CardGivecardLog();
            $zs_card_id = json_decode($arr['zs_card_id_all'],true);
            $data = [];
            foreach ($zs_card_id as $k => $v) {
                $data[$k]['c_id'] = $arr['c_id'];
                $data[$k]['us_c_id'] = $arr['us_c_id'];
                $data[$k]['card_id'] = $arr['card_id'];
                $data[$k]['openid'] = $arr['openid'];
                $data[$k]['code'] = $arr['code'];
                $data[$k]['put_type'] = 'birthdaycare';
                $data[$k]['zs_card_id'] = $v['zs_card_id'];
                $data[$k]['give_rec_id'] = $give_rec_id;
                $data[$k]['mid'] = $arr['mid'];
                $data[$k]['top_mid'] = $arr['top_mid'];
                $data[$k]['put_sume'] = $v['zs_no'];
                $data[$k]['create_time'] = time();
            }
            $res = $CardGivecardLog->allowField(true)->saveAll($data);
        }else{
            $res = 0;
        }
        return $give_rec_id;
    }



}
